package com.erp.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.erp.dto.Department;

@SuppressWarnings("serial")
@Entity
@Table(name = "t_department")
public class TDepartment implements java.io.Serializable {

	private Integer id;
	private String name;
	private String description;
	private Integer level = 1;
	private TDepartment parent;
	private List<TDepartment> children = new ArrayList<TDepartment>(0);

	public TDepartment() {
	}

	public TDepartment(String name, TDepartment parent) {
		this.name = name;
		this.parent = parent;
		if (parent != null) {
			this.level = parent.getLevel() + 1;
		}
	}

	public TDepartment(Department department) {
		this.id = department.getId();
		this.name = department.getName();
		this.description = department.getDescription();
	}

	@Id
	@GeneratedValue
	@Column(name = "id")
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "name")
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name = "level")
	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	/**
	 * parent属性，本类与TDepartment（上级）的多对一双向关系
	 * 
	 * @return
	 */
	@ManyToOne
	@JoinColumn(name = "parent_id")
	public TDepartment getParent() {
		return parent;
	}

	public void setParent(TDepartment parent) {
		this.parent = parent;
	}

	/**
	 * children属性，本类与TDepartment（下级）的一对多双向关系
	 * 
	 * @return
	 */
	@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
	public List<TDepartment> getChildren() {
		return children;
	}

	public void setChildren(List<TDepartment> children) {
		this.children = children;
	}

}